package com.wjc.http;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;

import com.wjc.http.util.Equal;
import com.wjc.http.util.IO;

 

public class HttpRequestFactory
{
 
    
    public static HttpRequest read(InputStream in, OutputStream out) throws IOException
    {
        HttpRequest httpRequest = new HttpRequest();
    	StringBuffer headerBody = new StringBuffer();
        
        byte[] bytes = null;
        
        byte[] LF = new byte[]{0x0A};
        byte[] CRLF = new byte[]{0x0D, 0x0A};
        
        if((bytes = IO.readLine(in)) != null)
        {
        	httpRequest.setFirstLine( new String(bytes));
        	headerBody.append(new String(bytes));
        	
            if(!Equal.equals(bytes, CRLF) && !Equal.equals(bytes, LF))
            {
                String header = new String(bytes).trim();
                
                java.util.StringTokenizer st = new java.util.StringTokenizer(header, " ");
                
                if(st.hasMoreTokens())
                {
                    httpRequest.setMethod(st.nextToken());
                }
                
                if(st.hasMoreTokens())
                {
                    String url = st.nextToken();
                  
                    httpRequest.setRequestURL(url);
                    
                    if(url != null && (url = url.trim()).length() > 0)
                    {
                        int k = url.indexOf("?");
                        
                        if(k < 0)
                        {
                            k = url.indexOf("&");
                        }
                        
                        if(k > -1)
                        {
                            httpRequest.setRequestURI(url.substring(0, k));
                            httpRequest.setQueryString(url.substring(k + 1));
                        }
                    }
                    else
                    {
                        url = "/";
                    }
                }
                
                if(st.hasMoreTokens())
                {
                    httpRequest.setHttpProtocol(st.nextToken());
                }
            }
        }
        
        while((bytes = IO.readLine(in)) != null)
        {
        	headerBody.append(new String(bytes));
            if(Equal.equals(bytes, CRLF) || Equal.equals(bytes, LF))
            {
                break;
            }
            
            String header = new String(bytes);
            
            int k = header.indexOf(":");
            
            if(k > -1)
            {
                String name  = header.substring(0, k).trim();
                String value = header.substring(k + 1).trim();
                
                httpRequest.setHeader(name, value);
            }
        }
        httpRequest.setHeaderBody( headerBody.toString() );
        
        httpRequest.setInputStream(in);
        httpRequest.setOutputStream(out);
        httpRequest.setShowHost(false);
        return httpRequest;
    }
    
 
}
